/*
 Based upon iniparser by Freek/2005.
 Converted into a C++ library for
 the PSP
 by Raf 9/2005.

 -- -

 Based upon libiniparser, by Nicolas Devillard
 Hacked into 1 file (m-iniparser) by Freek/2005
 Original terms following:

 -- -

 Copyright (c) 2000 by Nicolas Devillard (ndevilla AT free DOT fr).

 Written by Nicolas Devillard. Not derived from licensed software.

 Permission is granted to anyone to use this software for any
 purpose on any computer system, and to redistribute it freely,
 subject to the following restrictions:

 1. The author is not responsible for the consequences of use of
 this software, no matter how awful, even if they arise
 from defects in it.

 2. The origin of this software must not be misrepresented, either
 by explicit claim or by omission.

 3. Altered versions must be plainly marked as such, and must not
 be misrepresented as being the original software.

 4. This notice may not be removed or altered.

 */


#ifndef _INIPARSER_H_
#define _INIPARSER_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>

class CIniParser
{
public:
	CIniParser(const char* strIniFilename)
	{
		m_strIniFilename = strdup(strIniFilename);
		if(m_strIniFilename)
			m_dict = iniparser_new(m_strIniFilename);
	}
	~CIniParser()
	{
		iniparser_free(m_dict);
		if(m_strIniFilename)
		{
		    free(m_strIniFilename);
		    m_strIniFilename = NULL;
		}
	}

	void Save();

    char* m_strIniFilename;


	/** Accessors */
	//char *GetKey(char *section, char *key)
	//{
	//	return iniparser_getkey(m_dict, section, key);
	//}

	const char *GetStr(const char *key)
	{
		return iniparser_getstr(m_dict, key);
	}

	const char *GetString(const char *key, const char *def)
	{
		return iniparser_getstring(m_dict, key, def);
	}

	int GetInteger(const char *key, int notfound = -1)
	{
		return iniparser_getint(m_dict, key, notfound);
	}

	void SetInteger(const char *key, int value)
	{
		char tmp[10];
		sprintf(tmp, "%d", value);
		iniparser_setstr(m_dict, key, tmp);
	}

	void SetString(const char *key, const char *value)
	{
		iniparser_setstr(m_dict, key, value);
	}

private:
	struct dictionary
	{
		int n; /** Number of entries in dictionary */
		int size; /** Storage size */
		char **val; /** List of string values */
		char **key ; /** List of string keys */
		unsigned *hash; /** List of hash values for keys */
	};
	dictionary *m_dict;

	const char * strlwc(const char * s);
	const char * strupc(const char * s);
	const char * strskp(const char * s);
	const char * strcrop(const char * s);
	const char * strstrip(const char * s);
	void * mem_double(void * ptr, int size);
	unsigned dictionary_hash(const char * key);
	dictionary * dictionary_new(int size);
	void dictionary_del(dictionary * d);
	const char * dictionary_get(dictionary * d, const char * key, const char * def);
	void dictionary_set(dictionary * d, const char * key, const char * val);
	void dictionary_unset(dictionary * d, const char * key);
	void dictionary_dump(dictionary *d, FILE *f);
	void iniparser_add_entry(dictionary * d, const char * sec, const char * key, const char * val);
	int iniparser_getnsec(dictionary * d);
	const char * iniparser_getsecname(dictionary * d, int n);
	void iniparser_dump(dictionary * d, FILE * f);
	void iniparser_dump_ini(dictionary * d, FILE * f);
	const char * iniparser_getstr(dictionary * d, const char * key);
	const char * iniparser_getstring(dictionary * d, const char * key, const char * def);
	int iniparser_getint(dictionary * d, const char * key, int notfound);
	double iniparser_getdouble(dictionary * d, const char * key, double notfound);
	int iniparser_getboolean(dictionary * d, const char * key, int notfound);
	int iniparser_find_entry(dictionary  *   ini, const char        *   entry);
	int iniparser_setstr(dictionary * ini, const char * entry, const char * val);
	void iniparser_unset(dictionary * ini, const char * entry);
	dictionary * iniparser_new(const char *ininame);
	void iniparser_free(dictionary * d);

};

#endif

